package com.hech.hmall.entity;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author hanjiafeng
 * @date 2021/4/19 17:34
 * @desc todo
 */
@Data
@NoArgsConstructor
public class ResponseEntity<T> {

    private Integer code;
    private String msg;
    private T data;

    private ResponseEntity(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> ResponseEntity<T> success(String msg) {
        return new ResponseEntity<>(1, msg);
    }

    public static <T> ResponseEntity<T> failure(String msg) {
        return new ResponseEntity<>(-1, msg);
    }

    public static <T> ResponseEntity<T> failure(Code code, String msg) {
        return new ResponseEntity<>(code.getCode(), msg);
    }

    public static <T> ResponseEntity<T> failure(int code, String msg) {
        return new ResponseEntity<>(code, msg);
    }

    public void setData(T t) {
        data = t;
    }

    /**
     * 状态码
     */
    public enum Code {

        /**
         * 成功
         */
        SUCCESS(1),
        /**
         * 失败
         */
        FAILURE(-1);

        /**
         * 状态码
         */
        private final int code;

        /**
         * 状态
         *
         * @param code 状态码
         */
        Code(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }
    }
}
